import Vue from 'vue'
import VueRouter from 'vue-router'
import Login from '../components/login'
import Home from '../views/Home/index.vue'
import Welcome from '../components/welcome.vue'
import Users from '../views/Home/Users/users.vue'
Vue.use(VueRouter)

const router = new VueRouter({
  routes: [
    {
      path: '/',
      redirect: '/login'
    },
    {
      path: '/login',
      name: 'Login',
      component: Login
    },
    {
      path: '/home',
      name: Home,
      component: Home,
      redirect: '/welcome',
      children: [
        {
          path: '/welcome',
          component: Welcome
        },
        {
          path: '/users',
          component: Users
        }
      ]
    }
  ]
})
router.beforeEach((to, before, next) => {
  if (to.path === '/login') next()
  if (!window.sessionStorage.token) {
    next('/login')
  }
  next()
})

export default router
